package org.soenea.ass2.application;

import java.io.IOException;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.soenea.ass2.services.datasource.UserRDG;


/**
 * Servlet implementation class LoginTS
 */

public class LoginTS extends SampleHttpServlet {
	private static final long serialVersionUID = 1L;


	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginTS() {
        super();
        // TODO Auto-generated constructor stub
    }
    
	protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		try {
			
		
			UserRDG user = UserRDG.find(request.getParameter("user_name"));
	
			String password = request.getParameter("password");
			
						
			if(user.getPassword().equals(password)){
	
				request.getSession().setAttribute("user", user);
				request.getSession().setAttribute("logged", Boolean.TRUE);
				request.getRequestDispatcher("dash.jsp").forward(request, response);
	
			}
			
			else {
				
				request.getSession().setAttribute("logged", Boolean.FALSE);
				request.getRequestDispatcher("dash.jsp").forward(request, response);
				
			}
			
		}
		
		catch (Exception SQLException){
			
			request.getSession().setAttribute("logged", Boolean.FALSE);
			request.getRequestDispatcher("dash.jsp").forward(request, response);	
			
		}
		
	}


	
}
